//
//  YWItemButton.m
//  YunWu
//
//  Created by 古俊杰 on 16/4/30.
//  Copyright © 2016年 曲虹亮. All rights reserved.
//

#import "YWItemButton.h"

#define RGB(r, g, b)      [UIColor colorWithRed:r/255.0 green:g/255.0 blue:b/255.0 alpha:1]

// 图标的比例
//#define IWTabBarButtonImageRatio 0.8

@interface YWItemButton ()
{
    BOOL _fromeNib;
    CGFloat _tabBarButtonImageRatio;
}

@end


@implementation YWItemButton

-(void)awakeFromNib
{
    [super awakeFromNib];
    
    _fromeNib = YES;
    _tabBarButtonImageRatio = 0.5;
    self.imageView.contentMode = UIViewContentModeScaleAspectFit;
    // 文字居中
    self.titleLabel.textAlignment = NSTextAlignmentCenter;
    // 字体大小
    self.titleLabel.font = [UIFont systemFontOfSize:10];
    // 文字颜色
    [self setTitleColor:RGB(143, 143, 148) forState:UIControlStateNormal];
    
}

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        _tabBarButtonImageRatio = 0.8;
        // 图标居中

        self.imageView.contentMode = UIViewContentModeScaleAspectFit;
        //        self.imageView.layer.cornerRadius = 6.0;

        
        // 文字居中
        self.titleLabel.textAlignment = NSTextAlignmentCenter;
        // 字体大小
        self.titleLabel.font = [UIFont systemFontOfSize:14];
        // 文字颜色
        [self setTitleColor:RGB(143, 143, 148) forState:UIControlStateNormal];

    }
    
    return self;
}

//-(void)setHighlighted:(BOOL)highlighted
//{
//    
//}

// 内部图片的frame
-(CGRect)imageRectForContentRect:(CGRect)contentRect
{
    CGFloat imageW = contentRect.size.width;
    CGFloat imageH = contentRect.size.height * _tabBarButtonImageRatio;
    
    if (_fromeNib) {
        return CGRectMake(0, 6, imageW, imageH);
    } else {
        return CGRectMake(0, 0, imageW, imageH);
    }

}

// 内部文字的frame
- (CGRect)titleRectForContentRect:(CGRect)contentRect
{
    CGFloat titleY = contentRect.size.height * _tabBarButtonImageRatio;
    CGFloat titleW = contentRect.size.width + 10;
    CGFloat titleH = contentRect.size.height - titleY;
    if (_fromeNib) {
         return CGRectMake(1, titleY + 4, contentRect.size.width, titleH);
        
    } else if (self.isFromeMyShare) {
        return CGRectMake(-5, titleY + 8, titleW, titleH);
    }
    else {
         return CGRectMake(-5, titleY + 6, titleW, titleH);
    }
   
}

-(void)setItem:(UITabBarItem *)item
{
    _item = item;
    
    // KVO 监听属性改变---实现外面可以动态改变这些属性
    [item addObserver:self forKeyPath:@"title" options:0 context:nil];
    [item addObserver:self forKeyPath:@"image" options:0 context:nil];
    [item addObserver:self forKeyPath:@"selectedImage" options:0 context:nil];
    
    [self observeValueForKeyPath:nil ofObject:nil change:nil context:nil];
}


- (void)dealloc
{
    [self.item removeObserver:self forKeyPath:@"title"];
    [self.item removeObserver:self forKeyPath:@"image"];
    [self.item removeObserver:self forKeyPath:@"selectedImage"];
}

/**
 *  监听到某个对象的属性改变了,就会调用
 *
 *  @param keyPath 属性名
 *  @param object  哪个对象的属性被改变
 *  @param change  属性发生的改变
 */
-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
    // 设置文字
    [self setTitle:self.item.title forState:UIControlStateSelected];
    [self setTitle:self.item.title forState:UIControlStateNormal];
    
    // 设置图片
    [self setImage:self.item.image forState:UIControlStateNormal];
    [self setImage:self.item.selectedImage forState:UIControlStateSelected];

}


@end
